﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Storage;
using Storage.EntityModels;
using System.Data.Linq;
using Stats.Objects;

namespace Stats
{
    /// <summary>
    /// class that contains the method to retreive the userprofile
    /// </summary>
    public static class UserProfileModule
    {
        /// <summary>
        /// This method contains the necessary operations to retrieve user profile and returns 
        /// an UserProfile object which contains all statistics about the user.
        /// </summary>
        /// <param name="userID">UserID</param>
        /// <returns>An UserProfile object</returns>
        public static UserProfile getUserProfileOperation(int userID)
        {

            UserSavings usrSv = AuxUserMethods.getUserSavings(userID);
            
            UserStats uSt = new UserStats(userID, AuxDistanceMethods.getUserTotalKmAsDriver(userID) + AuxDistanceMethods.getUserTotalKmAsPassenger(userID), usrSv.UserFuelSaved,
                                            usrSv.UserMoneySaved, usrSv.UserEmissionsSaved, AuxUserMethods.getTripsAsDriverCount(userID), AuxUserMethods.getTripsAsPassengerCount(userID),
                                            AuxUserMethods.getUserTripsProposedNumber(userID), AuxUserMethods.getUserAsDriverMissedAppointmentsCount(userID), AuxUserMethods.getUserAsPassengerMissedAppointmentsCount(userID), AuxUserMethods.getUserTotalCarriedPassengersCount(userID),
                                            AuxUserMethods.getMaxPassengerCarriedInATrip(userID), AuxUserMethods.getPassengersCarriedOnTheWayCount(userID));

            AsDriverFeedback fdbDr;
            AsPassengerFeedback fdbPsn;

            try
            {
                TripLoaDataContext db = StorageManager.Instance.Entities;

                var userFeedbackTable =
                     from userFeedbackEntries in db.UserFeedbacks
                     where userFeedbackEntries.UserId == userID
                     select userFeedbackEntries;

                var userEntry = userFeedbackTable.First();

                fdbDr = new AsDriverFeedback(userID, userEntry.AsDriverAVGVote, userEntry.AsDriverAVGPunctuality, userEntry.AsDriverAVGReliability, userEntry.AsDriverAVGKindness, userEntry.NumberOfJourneysWithFeedbackAsDriver);
                fdbPsn = new AsPassengerFeedback(userID, userEntry.AsPassengerAVGVote, userEntry.AsPassengerAVGPunctuality, userEntry.AsPassengerAVGReliability, userEntry.NumberOfJourneysWithFeedbackAsPassenger);
            }
            catch (InvalidOperationException)
            {
                fdbDr = new AsDriverFeedback(0, 0, 0, 0, 0, 0);
                fdbPsn = new AsPassengerFeedback(0, 0, 0, 0, 0);
            }

            catch (TypeInitializationException)
            {
                fdbDr = new AsDriverFeedback(0, 0, 0, 0, 0, 0);
                fdbPsn = new AsPassengerFeedback(0, 0, 0, 0, 0);
            }

            catch (NullReferenceException)
            {
                fdbDr = new AsDriverFeedback(0, 0, 0, 0, 0, 0);
                fdbPsn = new AsPassengerFeedback(0, 0, 0, 0, 0);
            }

            catch (System.Data.SqlClient.SqlException)
            {
                fdbDr = new AsDriverFeedback(0, 0, 0, 0, 0, 0);
                fdbPsn = new AsPassengerFeedback(0, 0, 0, 0, 0);
            }

            UserProfile usP = new UserProfile(fdbDr, fdbPsn, uSt);

            return usP;

        }
                

    }
}
